package com.snimay.app.entity;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.snimay.app.entity.repository.ItemsRepository;
import com.snimay.app.user.vo.User;
import com.snimay.javacompiler.compiler.JavaFile;
import com.snimay.util.Status;
import com.snimay.util.ToWeb;

/**   
 * @Title      : ItemsController.java
 * @Package    : com.snimay.app.entity
 * @Description: TODO 
 * @author     : xxy
 * @date       : 2018年5月26日 下午12:01:09
 * @version    : V1.0   
 */
@RestController
@RequestMapping("items")
@Scope("session")
public class ItemsController {
	
	private SessionFactory sessionFactory;
	
	@Autowired
	ItemsRepository itemsRepository;
	@Autowired
	JavaFile javaFile;  
	
	static String packages="com.snimay.app.entity.vo.";
	
	
	
	public void setSeesionFactory(SessionFactory seesionFarctory) {
		System.out.println("xxydfdfjfjfjdkd");
		this.sessionFactory	=	seesionFarctory;
	}
	
	public void setI(String i) {
		System.out.println(i);
	}
	
	/**
	 * @Description: 用于获取数据.
	 * @author     : xxy
	 * @param name
	 * @return
	 * @throws ClassNotFoundException
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws
	 */
	@GetMapping("/{name}/")
	public Object get(@PathVariable("name") String name) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
		ToWeb toweb = new ToWeb();
		toweb.setObjData(itemsRepository.seach(sessionFactory,Class.forName(packages+name).newInstance(), "1=1"));
		return new Gson().toJson(toweb);
	}
	
	/**
	 * @Description: 将实体提交到指定的资源轻武器
	 * @author     : xxy
	 * @param name
	 * @param entity
	 * @return
	 * @throws ClassNotFoundException
	 * @throws
	 */
	@PostMapping("/{name}/")
	public Object post(@PathVariable("name") String name,@RequestBody String entity) throws ClassNotFoundException {
		ToWeb toweb = new ToWeb();
		toweb.setObjData(itemsRepository.save(sessionFactory,new Gson().fromJson(entity, Class.forName(packages+name))));
		return new Gson().toJson(toweb);
	}
	/**
	 * @Description: 对资源进行整体覆盖
	 * @author     : xxy
	 * @param name
	 * @param entity
	 * @return
	 * @throws JsonSyntaxException
	 * @throws ClassNotFoundException
	 * @throws
	 */
	@PutMapping("/{name}/")
	public Object put(@PathVariable("name") String name,@RequestBody String entity) throws JsonSyntaxException, ClassNotFoundException {
		ToWeb toweb = new ToWeb();
		toweb.setObjData(itemsRepository.saveOrUpdate(sessionFactory,new Gson().fromJson(entity, Class.forName(packages+name))));
		return new Gson().toJson(toweb);
	} 
	
	/**
	 * @Description: 对资源部分修改
	 * @author     : xxy
	 * @param name
	 * @param entity
	 * @return
	 * @throws JsonSyntaxException
	 * @throws ClassNotFoundException
	 * @throws
	 */
	@PatchMapping("/{name}/")
	public Object patch(@PathVariable("name") String name,@RequestBody String entity) throws JsonSyntaxException, ClassNotFoundException {
		ToWeb toweb = new ToWeb();
		toweb.setObjData(itemsRepository.saveOrUpdate(sessionFactory,new Gson().fromJson(entity, Class.forName(packages+name))));
		return new Gson().toJson(toweb);
	}
	
	/**
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 * @Description: 删除指定的资源
	 * @author     : xxy
	 * @param name
	 * @param entity
	 * @return
	 * @throws JsonSyntaxException
	 * @throws ClassNotFoundException
	 * @throws
	 */
	@DeleteMapping("/{name}/{id}")
	public Object delecte(@PathVariable("name") String name,@PathVariable("id") String id) throws JsonSyntaxException, ClassNotFoundException, InstantiationException, IllegalAccessException {
		ToWeb toweb = new ToWeb();
		itemsRepository.delecte(sessionFactory,Class.forName(packages+name).newInstance(),id);
		return new Gson().toJson(toweb);
	}
/*	String code = 
		  "package com.snimay.app.role.vo;"
		+ "import javax.persistence.Entity;"
		+ "import javax.persistence.GeneratedValue;"
		+ "import javax.persistence.GenerationType;"
		+ "import javax.persistence.Id;"
		+ "import javax.persistence.Table;"
		+ "@Entity @Table(name = \"SYS_Role2\") "
		+ "public class Role2{"
			+ "@Id "
			+ "@GeneratedValue(strategy = GenerationType.IDENTITY)"
			+ "public Long id;"
			+ "public String rolename;"
			+ "public String remarks;"
		+ "}";
	@GetMapping("/{name}")
	public Object getOne(@PathVariable("name") String name) throws Exception {
		ToWeb toweb = new ToWeb();
		javaFile.setFile(code);
		return new Gson().toJson(toweb);
	}*/
}
